<?php

include '../templateSys/SQL.php';
include '../templateSys/Settings.php';
include '../templateSys/manager_util.php';
include '../templateSys/Admin.php';

global $link;
$link = getLink();

if (isset($_GET["size"])) {
    $size = $_GET["size"];
} else {
    exit();
}

if (isset($_GET["id"])) {
    $id = $_GET["id"];
} else {
    exit();
}

$query = sprintf(SQL::$update_img_byName, mysql_real_escape_string($id)
);
mq($query, $link);

if ($size == "small") {
    $query = sprintf(SQL::$select_img_byName_small, mysql_real_escape_string($id)
    );
    $result = mq($query, $link);
    while ($row = mysql_fetch_array($result)) {
        setHeader($row["ext"], base64_decode($row["smallSize"]));
    }
}
if ($size == "medium") {
    $query = sprintf(SQL::$select_img_byName_medium, mysql_real_escape_string($id)
    );
    $result = mq($query, $link);
    while ($row = mysql_fetch_array($result)) {
        setHeader($row["ext"], base64_decode($row["mediumSize"]));
    }
}
if ($size == "full") {
    $query = sprintf(SQL::$select_img_byName_full, mysql_real_escape_string($id)
    );
    $result = mq($query, $link);
    while ($row = mysql_fetch_array($result)) {
        setHeader($row["ext"], base64_decode($row["fullSize"]));
    }
}

mysql_close($link);

function setHeader($ext, $db_img) {
    //header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
    switch ($ext) {
        case "JPG":
            header("Content-Type: image/jpeg");
            $db_img = imagecreatefromstring($db_img);
            imagejpeg($db_img);
            break;
        case "jpg":
            header("Content-Type: image/jpeg");
            $db_img = imagecreatefromstring($db_img);
            imagejpeg($db_img);
            break;
        case "jpeg":
            header("Content-Type: image/jpeg");
            $db_img = imagecreatefromstring($db_img);
            imagejpeg($db_img);
            break;
        case "gif":
            header("Content-Type: image/gif");
            $db_img = imagecreatefromstring($db_img);
            imagegif($db_img);
            break;
        case "png":
            header("Content-Type: image/png");
            $db_img = imagecreatefromstring($db_img);
            imagepng($db_img);
            break;
    }
    imagedestroy($db_img);
}

?>
